const { createWebAPIRequest } = require('../../util/util')

module.exports = [
  {
    /**
    * @api {get} /music/top/album [新碟上架]
    * @apiGroup Music
    * @apiPermission none
    * @apiDescription 新碟上架 http://music.163.com/#/discover/album/
    * @apiParam {Number} [offset=0]  页码
    * @apiParam {Number} [limit=30]  每页数量
    * @apiParam {String} [type=ALL]  类型
    */
    url: '/music/top/album',
    method: 'get',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      // type ALL, ZH,EA,KR,JP
      const data = {
        offset: req.query.offset || 0,
        total: true,
        limit: req.query.limit || 30,
        area: req.query.type || 'ALL',
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        '/weapi/album/new',
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/top/artists [热门歌手]
    * @apiGroup Music
    * @apiPermission none
    * @apiDescription 热门歌手 http://music.163.com/#/discover/artist/
    * @apiParam {Number} [offset=0]  页码
    * @apiParam {Number} [limit=30]  每页数量
    */
    url: '/music/top/artists',
    method: 'get',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const data = {
        offset: req.query.offset || 0,
        total: true,
        limit: req.query.limit || 30,
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        `/weapi/artist/top`,
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/top/list [热门歌榜]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {String} idx '0': ['云音乐新歌榜'],
        '1': ['云音乐热歌榜'],
        '2': ['网易原创歌曲榜'],
        '3': ['云音乐飙升榜'],
        '4': ['云音乐电音榜'],
        '5': ['UK排行榜周榜'],
        '6': ['美国Billboard周榜'],
        '7': ['KTV嗨榜'],
        '8': ['iTunes榜'],
        '9': ['Hit FM Top榜'],
        '10': ['日本Oricon周榜'],
        '11': ['韩国Melon排行榜周榜'],
        '12': ['韩国Mnet排行榜周榜'],
        '13': ['韩国Melon原声周榜'],
        '14': ['中国TOP排行榜(港台榜)'],
        '15': ['中国TOP排行榜(内地榜)'],
        '16': ['香港电台中文歌曲龙虎榜'],
        '17': ['华语金曲榜'],
        '18': ['中国嘻哈榜'],
        '19': ['法国 NRJ EuroHot 30周榜'],
        '20': ['台湾Hito排行榜'],
        '21': ['Beatport全球电子舞曲榜'],
        '22': ['云音乐ACG音乐榜'],
        '23': ['云音乐嘻哈榜']
    */
    url: '/music/top/list',
    method: 'get',
    request: (req, res) => {
      const topListAll = {
        '0': ['云音乐新歌榜', '3779629'],
        '1': ['云音乐热歌榜', '3778678'],
        '2': ['网易原创歌曲榜', '2884035'],
        '3': ['云音乐飙升榜', '19723756'],
        '4': ['云音乐电音榜', '10520166'],
        '5': ['UK排行榜周榜', '180106'],
        '6': ['美国Billboard周榜', '60198'],
        '7': ['KTV嗨榜', '21845217'],
        '8': ['iTunes榜', '11641012'],
        '9': ['Hit FM Top榜', '120001'],
        '10': ['日本Oricon周榜', '60131'],
        '11': ['韩国Melon排行榜周榜', '3733003'],
        '12': ['韩国Mnet排行榜周榜', '60255'],
        '13': ['韩国Melon原声周榜', '46772709'],
        '14': ['中国TOP排行榜(港台榜)', '112504'],
        '15': ['中国TOP排行榜(内地榜)', '64016'],
        '16': ['香港电台中文歌曲龙虎榜', '10169002'],
        '17': ['华语金曲榜', '4395559'],
        '18': ['中国嘻哈榜', '1899724'],
        '19': ['法国 NRJ EuroHot 30周榜', '27135204'],
        '20': ['台湾Hito排行榜', '112463'],
        '21': ['Beatport全球电子舞曲榜', '3812895'],
        '22': ['云音乐ACG音乐榜', '71385702'],
        '23': ['云音乐嘻哈榜', '991319590']
      }
      const idx = req.query.idx
      const id = topListAll[idx][1]
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const action = '/weapi/v3/playlist/detail'
      const data = {
        id,
        n: 10000,
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        action,
        'POST',
        data,
        cookie,
        musicReq => {
          res.setHeader('Content-Type', 'application/json')
          // console.log(JSON.parse(musicReq).playlist.tracks.length)
          // console.log(JSON.parse(musicReq).playlist.trackIds.length)
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/top/mv [热门MV]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} [offset=0]  页码
    * @apiParam {Number} [limit=30]  每页数量
    */
    url: '/music/top/mv',
    method: 'get',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const data = {
        offset: req.query.offset || 0,
        total: true,
        limit: req.query.limit || 30,
        csrf_token: ''
      }

      createWebAPIRequest(
        'music.163.com',
        '/weapi/mv/toplist',
        'POST',
        data,
        cookie,
        musicReq => {
          res.setHeader('Content-Type', 'application/json')
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/top/playlist [榜单分类歌单]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} [offset=0]  页码
    * @apiParam {Number} [limit=30]  每页数量
    * @apiParam {String} [order=hot] order可为 'hot' 可为 'new'
    * @apiParam {String} [cat=全部] 分类
    * @apiParam {Boolean} [total=false]
    */
    url: '/music/top/playlist',
    method: 'get',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      // order可为 'hot' 可为 'new'
      const data = {
        cat: req.query.cat || '全部',
        order: req.query.order || 'hot',
        offset: req.query.offset || 0,
        total: req.query.total ? 'true' : 'false',
        limit: req.query.limit || 30
      }
      createWebAPIRequest(
        'music.163.com',
        '/weapi/playlist/list',
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/top/playlist/highquality [精品歌单]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {String} [cat=全部]
    * @apiParam {Number} [offset=0]  页码
    * @apiParam {Number} [limit=20]  每页数量
    */
    url: '/music/top/playlist/highquality',
    method: 'get',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const data = {
        cat: req.query.cat || '全部',
        offset: req.query.offset || 0,
        limit: req.query.limit || 20,
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        '/weapi/playlist/highquality/list',
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/top/song [热门歌曲]
    * @apiGroup Music
    * @apiPermission none
    * @apiDescription 暂时废弃
    * @apiParam {Number} [offset=0]  页码
    * @apiParam {Number} [limit=20]  每页数量
    */
    url: '/music/top/song',
    method: 'get',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const data = {
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        '/weapi/v1/discovery/new/songs',
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }
]
